package com.wntms.zjy.service.impl;

import com.github.pagehelper.PageHelper;
import com.wntms.zjy.dao.CustomerReplyDaoz;
import com.wntms.zjy.dao.FreightContractDaoz;
import com.wntms.zjy.pojo.CustomerReply;
import com.wntms.zjy.pojo.FreightContract;
import com.wntms.zjy.receive.SendSMS;
import com.wntms.zjy.service.CustomerReplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author xzbz
 * @create 2022-11-22 16:19
 */
@Service
public class CustomerReplyServiceImpl implements CustomerReplyService {
    @Autowired
    private CustomerReplyDaoz customerReplyDao;
    @Autowired
    private FreightContractDaoz freightContractDao;

    @Override
    public List<CustomerReply> findAll() {
        List<CustomerReply> customerReplies = customerReplyDao.findAll();

        return customerReplies;
    }

    @Override
    public CustomerReply findByFid(String fid) {
        return customerReplyDao.findByFid(fid);
    }

    @Override
    @Transactional
    public boolean addNewReply(CustomerReply customerReply) {

        //setFc
        FreightContract freightContract = freightContractDao.findById(customerReply.getFid());
        customerReply.setFc(freightContract);

        Integer status = customerReply.getFc().getFreightStatus().getStatus();
        String newStatus = "";
        switch (status){
            case 0:
                newStatus = "待发货";
                break;
            case 1:
                newStatus = "运输中";
                break;
            case 2:
                newStatus = "正在中转";
                break;
            case 3:
                newStatus = "已到货";
                break;
            case 4:
                newStatus = "可以提货了";
                break;
            case 5:
                newStatus = "已提";
                break;
        }

        String[] send = SendSMS.send(customerReply.getFc().getCustomerIn().getTel(),
                customerReply.getFc().getCustomerIn().getName(), newStatus);
        //set通知内容
        customerReply.setNoticeContent("尊敬的"+customerReply.getFc().getCustomerIn().getName()+"先生（女士）你好，您的货物"+newStatus);

        int num = customerReplyDao.addNewReply(customerReply);
        return num == 1 ? true:false;
    }

    @Override
    public boolean deleteReply(Integer id) {
        int num = customerReplyDao.deleteReply(id);
        return num == 1 ? true:false;
    }

    @Override
    public List<CustomerReply> findByPage(Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage,pageSize);

        List<CustomerReply> customerReplies = customerReplyDao.findAll();
        return customerReplies;
    }
}
